/**
 * Created by zhangguanggui on 2017/7/22.
 */

export {addClass, hasCalss, handleAttr};

function addClass (el, className) {
  if (hasCalss(el, className)) {
    return;
  }
  let classArr = el.className.split(' ');
  classArr.push(className);
  el.className = classArr.join(' ');
}

function hasCalss (el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
  return reg.test(el);
}

function handleAttr (el, name, v) {
  const prefix = 'data-';
  name = prefix + name;
  if (!v) {
    return el.getAttribute(name);
  } else {
    return el.setAttribute(name, v);
  }
}

/*
*  浏览器前缀封装
* */
let elStyles = document.createElement('div').style;
let vendor = (() => {
  let prefix = {
    webkit: 'webkitTransform',
    Moz: 'mozTransform',
    ms: 'msTransform',
    O: 'OTransform',
    standard: 'transform'
  };
  for (let key in prefix) {
    if (elStyles[prefix[key]] !== undefined) {
      return key;
    }
  }
  return false;
})();

export function prefixStyle (style) {
  if (vendor === false) {
    return false;
  }
  if (vendor === 'standard') {
    return style;
  }
  return vendor + style.charAt(0).toUpperCase() + style.substring(1);
}

